<template>
  <view class="search-container">
    <!-- 搜索 -->
    <view class="search-box"  @click="clearn">
      <uni-search-bar @input="input" :radius="100" cancelButton="none">
      </uni-search-bar>
    </view>
    <!-- 渲染搜索 -->
    <view class="sugg-list" v-if="goodsList.length > 0">
      <view v-for="item in goodsList" :key="item.goods_id" class="sugg-item">
        <text class="goods-name" @click="goToDetail(item.goods_id)">{{
          item.goods_name
        }}</text>
      </view>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" @click="clear"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag @click="goToGoosList(item)" v-for="item in historyList" :key="item" :text="item"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      goodsList: [], // 商品列表
      historyList: [], // 搜索历史
   
    };
  },
  onLoad() {
    this.historyList = uni.getStorageSync("history") || [];
 
  },
  methods: {
    // 防抖
    input(e) {
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        this.getGoodsListData(e);
      }, 500);
    },
    // 搜索数据
    async getGoodsListData(keyword) {
      if (keyword.trim().length === 0) return;
      const res = await uni.$request({
        url: "goods/qsearch",
        data: {
          query: keyword,
        },
      });
      // console.log(res);
      if (res.meta.status === 200) {
        this.goodsList = res.message;
      }
      this.saveHistory(keyword);
    },

    saveHistory(keyword) {
      // // 查询是否存在,如果有 ,则删除
      // this.historyList.unshift(keyword)
      // // 去重操作
      // this.historyList=[...new Set(this.historyList)]
      // // 保存历史数据到本地
      // uni.setStorageSync('history', this.historyList)
      // ********************************
      // 查找是否有存在的，如果有，则删掉
      const index = this.historyList.findIndex((item) => item === keyword);
      if (index > -1) {
        this.historyList.splice(index, 1);
      }
      // 把关键字存储到historyList中
      this.historyList.unshift(keyword);

      uni.setStorageSync("history", this.historyList);
    },
    // 清空搜索历史数据
    clear() {
      this.historyList = [] 
      uni.setStorageSync("history", []);
    },
    clearn(){
       this.goodsList = [] 
    },
    // 根据搜索的结果 点击跳转页面
    goToDetail(goods_id) {
      uni.navigateTo({
        url: "/subpkg/goods-detail/index?goods_id=" + goods_id,
      });
    },
    // 点击关键字
    goToGoosList(item){
      uni.navigateTo({  url: "/subpkg/goods-list/index?query=" + item })
      
    }
  },
};
</script>


<style lang="scss" scoped>
.search-container {
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    ::v-deep .uni-searchbar {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      position: relative;
      padding: 16rpx;
      /* 将默认的 #FFFFFF 改为 #C00000 */
      background-color: #c00000;
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      ::v-deep .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }
  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
}
</style>